﻿
/**
 * 默认功能
 * 在'image', 后面去了'multiimage',
 */
var defaultItems = [
 		'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
 		'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
 		'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
 		'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
 		'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
 		'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image',
 		'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
 		'anchor', 'link', 'unlink', '|', 'about'
 	];

/**
 * simple
 */
var simpleItems = [
	 'source', '|', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
	 'italic', 'underline', 'strikethrough', 'removeformat', '|', 
	 'justifyleft', 'justifycenter', 'justifyright','justifyfull', '|', 'link', 'unlink', '|', 'about'
];

/**
 * mesage
 */
var msgItems = [
	 'source', '|', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
	 'italic', 'underline', 'strikethrough', 'removeformat','|', 'link', 'unlink', '|', 'about'
];


/**
 * 216种颜色
 */
var colorTable = [
['#000000','#000033','#000066','#000099','#0000CC','#0000FF','#330000','#330033','#330066','#330099','#3300CC','#3300FF','#003300','#003333','#003366','#003399','#0033CC','#0033FF'],
['#333300','#333333','#333366','#333399','#3333CC','#3333FF','#006600','#006633','#006666','#006699','#0066CC','#0066FF','#336600','#336633','#336666','#336699','#3366CC','#3366FF'],
['#009900','#009933','#009966','#009999','#0099CC','#0099FF','#339900','#339933','#339966','#339999','#3399CC','#3399FF','#00CC00','#00CC33','#00CC66','#00CC99','#00CCCC','#00CCFF'],
['#33CC00','#33CC33','#33CC66','#33CC99','#33CCCC','#33CCFF','#00FF00','#00FF33','#00FF66','#00FF99','#00FFCC','#00FFFF','#33FF00','#33FF33','#33FF66','#33FF99','#33FFCC','#33FFFF'],
['#660000','#660033','#660066','#660099','#6600CC','#6600FF','#990000','#990033','#990066','#990099','#9900CC','#9900FF','#663300','#663333','#663366','#663399','#6633CC','#6633FF'],
['#993300','#993333','#993366','#993399','#9933CC','#9933FF','#666600','#666633','#666666','#666699','#6666CC','#6666FF','#996600','#996633','#996666','#996699','#9966CC','#9966FF'],
['#669900','#669933','#669966','#669999','#6699CC','#6699FF','#999900','#999933','#999966','#999999','#9999CC','#9999FF','#66CC00','#66CC33','#66CC66','#66CC99','#66CCCC','#66CCFF'],
['#99CC00','#99CC33','#99CC66','#99CC99','#99CCCC','#99CCFF','#66FF00','#66FF33','#66FF66','#66FF99','#66FFCC','#66FFFF','#99FF00','#99FF33','#99FF66','#99FF99','#99FFCC','#99FFFF'],
['#CC0000','#CC0033','#CC0066','#CC0099','#CC00CC','#CC00FF','#FF0000','#FF0033','#FF0066','#FF0099','#FF00CC','#FF00FF','#CC3300','#CC3333','#CC3366','#CC3399','#CC33CC','#CC33FF'],
['#FF3300','#FF3333','#FF3366','#FF3399','#FF33CC','#FF33FF','#CC6600','#CC6633','#CC6666','#CC6699','#CC66CC','#CC66FF','#FF6600','#FF6633','#FF6666','#FF6699','#FF66CC','#FF66FF'],
['#CC9900','#CC9933','#CC9966','#CC9999','#CC99CC','#CC99FF','#FF9900','#FF9933','#FF9966','#FF9999','#FF99CC','#FF99FF','#CCCC00','#CCCC33','#CCCC66','#CCCC99','#CCCCCC','#CCCCFF'],
['#FFCC00','#FFCC33','#FFCC66','#FFCC99','#FFCCCC','#FFCCFF','#CCFF00','#CCFF33','#CCFF66','#CCFF99','#CCFFCC','#CCFFFF','#FFFF00','#FFFF33','#FFFF66','#FFFF99','#FFFFCC','#FFFFFF']
];

/**
 * 显示在线编辑器
 * @param options
 */
function showWebEditor(options){
	var defaults = {
			channel         : "WEB"//上传前台的渠道，值见枚举Channel名
	};
	options = jQuery.extend({}, defaults, options);
	var count = 0;
	loadFile({url:[ctx+'/kindeditor/kindeditor-min.js'],
		callBack:function(){
			showEditor(options,count);
		},
		id:'kindeditorJs'});
}

function showEditor(options,count){
	if(typeof(KindEditor)=="undefined"){
		//若10秒内无法加载在线编辑器的js，抛出提示
		if(count<100){
			setTimeout(function(){showEditor(options,count);},100);
		}else{
			alert("无法加载在线编辑器，请检查网络！");
		}
		count++;
		return;
	}
	var id = options.id;
	var items;
	if("Simple"==options.type){
		items = simpleItems;
	}else if("Message"==options.type){
		items = msgItems;
	}else{
		items = defaultItems;
	}
	//--to add by xionggang date:20120609
	var newlineTag='p';
	if(options.config!=null&&options.config!=undefined){
		var configParam = evalJSON(options.config);
		if(configParam!=null){
			if(configParam.newlineTag!=null&&configParam.newlineTag!=undefined){
				newlineTag=configParam.newlineTag;
			}
			if(configParam.items!=null&&configParam.items!=undefined){
				items=configParam.items.split(",");
			}			
		}
	}//--end

	if(KindEditor.basePath==""||KindEditor.basePath==null){
		KindEditor.basePath = getKindEditorBasePath();
	}
	var config = {
        afterCreate: function(){
        	var jHidden = _getJQueryById(id);
        	var maxLength = 0;
        	var initLen = 0;
        	var param = jHidden.attr("parameter");
        	if(param!=null){
        		param = evalJSON(param);
        		if(param.maxLen!=null){
        			maxLength = parseInt(param.maxLen,10);
        			this.contentMaxLength = parseInt(param.maxLen,10);
        			initLen = getStringLength(jHidden.val());
        		}
        	}
        	if(maxLength > 0 && _getJQueryById("#"+id+"_Flag").length==0){
        		var div = "<div id=\""+id+"_Flag\">源码字节数（一个汉字占3个字节）：<span id=\""+id+"_FieldCount\" style=\"color:green;\">"+initLen+"</span>/<span style=\"color:red;\">"+maxLength+"</span>&nbsp;字节&nbsp;<span id=\""+id+"_Message\" style=\"display:none; color:red;\">（长度超限，请控制在"+maxLength+"字节以内）</span></div>";
        		jHidden.after(div);
        	}
        	if(KindEditor.instances==null){
        		KindEditor.instances = new Array();
        	}
        	if(KindEditor.instances[id]!=null){
        		delete KindEditor.instances[id];
        	}
        	KindEditor.instances[id]=this;
        },
        afterChange: function(){
        	var length = getStringLength(this.html());
	        _getJQueryById("#"+id+"_FieldCount").text(length);
	        if(this.contentMaxLength-length < 0){
	        	_getJQueryById("#"+id+"_Message").show();
	        }else{
	        	_getJQueryById("#"+id+"_Message").hide();
	        }
        },
        uploadJson :mvc_ctx+"/sys/file.editorUploader?channel="+options.channel,
        fileManagerJson:mvc_ctx+"/sys/file.editorManager?channel="+options.channel,
        allowFileManager:true,
        syncType:"",
        items : items,
        colorTable : colorTable,
        newlineTag : newlineTag
	};
	var w = jQuery("#"+id).width();
	var h = jQuery("#"+id).height();
	if(w>0)config.width = w;
	else config.width = 700;
	if(h>0)config.height = h;
	else config.height = 200;
	KindEditor.create("#"+id,config);
}

function getKindEditorBasePath() {
	var els = document.getElementsByTagName('script'), src;
	for (var i = 0, len = els.length; i < len; i++) {
		src = els[i].src || '';
		if (/myKindEditor\.js/.test(src)) {
			return src.substring(0, src.lastIndexOf('/') + 1);
		}
	}
	return '';
}
function getStringLength(val){
//	var len = val.length;
//	var regexp = /[^\x00-\xff]+/g;
//	var result = val.match(regexp);
//	if(result!=null){
//		for(var i=0;i<result.length;i++){
//			len+=result[i].length*2;
//		}
//    }
	return getEditorStringLength(val); //调用core.js里的方法
}

/**
 * 在提交保存前，先对当前字段进行设值操作
 */
function prepareKindEditor(){
	if(typeof(KindEditor)!="undefined"&&KindEditor.instances!=undefined){
		for(var item in KindEditor.instances){
			var editor = KindEditor.instances[item];
			if(editor) editor.sync();
		}
	}
}

/**
 * 在重置时，将当前文本域里的内容，放到在线编辑器控件里
 */
function resetKindEditor(){
	if(typeof(KindEditor)!="undefined"&&KindEditor.instances!=undefined){
		for(var item in KindEditor.instances){
			var editor = KindEditor.instances[item];
			if(editor) editor.html(_getJQueryById(item).val());
		}
	}
}
